﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EZCRS.HCMS.Data;
using EZCRS.HCMS.Entity;
using System.Data;
using System.Data.SqlClient;

namespace EZCRS.HCMS.SqlClient
{
    public class sql_AdmNews
    {
        private string SqlStr = "";
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet News_List(int PageSize, int PageIndex, string StrWhere)
        {
            return ExecSql.AddParamFrKeyField("hfg_news", "*", "indate", "newid", PageSize, PageIndex, StrWhere);
        }
        ///<summary>
        ///分页获取数据总数
        ///</summary>
        public int Getrecordcount(string strWhere)
        {
            string sqlstring = "SELECT COUNT(*) as recordcount FROM hfg_news";
            if (strWhere != null && strWhere != "")
            {
                sqlstring += " WHERE " + strWhere;
            }
            DataSet ds = ExecSql.GetDataSet(sqlstring);
            return Convert.ToInt32(ds.Tables[0].Rows[0]["recordcount"]);
        }
        #region 新闻类别添加、修改
        /// <summary>
        /// 修改或添加新闻类别
        /// </summary>
        /// <param name="Gid">新闻类别id</param>
        /// <param name="Pid">新闻类别类型</param>
        /// <param name="ClsName">集团编号</param>
        /// <param name="ClsType">酒店编号</param>
        /// <param name="ClsRank">标题</param>
        /// <param name="InDate">小图</param>
        /// <param name="InUser">内容</param>
        /// <param name="ClsId">新闻类别编号 空 为新增，其他为修改</param>
        /// <returns></returns>
        public bool News_Sort_Add(string Gid, string Pid, string ClsName, string ClsType, string ClsRank, string InDate, string InUser, string ClsId)
        {
            SqlParameter[] pam = new SqlParameter[7];
            pam[0] = new SqlParameter("@gid", Gid);
            pam[1] = new SqlParameter("@pid", Pid);
            pam[2] = new SqlParameter("@clsname", ClsName);
            pam[3] = new SqlParameter("@clstype", ClsType);
            pam[4] = new SqlParameter("@clsrank", ClsRank);
            pam[5] = new SqlParameter("@indate", InDate);
            pam[6] = new SqlParameter("@inuser", InUser);

            if (ClsId != "")
                SqlStr = "update hfg_newsclass set gid=@gid,pid=@pid,clsname=@clsname,clstype=@clstype,clsrank=@clsrank,indate=@indate,inuser=@inuser where clsid=" + ClsId + "";
            else
                SqlStr = "insert into hfg_newsclass(gid,pid,clsname,clstype,clsrank,indate,inuser) values(@gid,@pid,@clsname,@clstype,@clsrank,@indate,@inuser)";
            if (ExecSql.SqlExecNoquery(SqlStr, pam) > 0)
                return true;
            else
                return false;
        }
        #endregion
        #region 新闻添加、修改
        /// <summary>
        /// 修改或添加新闻
        /// </summary>
        /// <param name="ClsId">新闻类别id</param>
        /// <param name="ClsType">新闻类别类型</param>
        /// <param name="Gid">集团编号</param>
        /// <param name="Pid">酒店编号</param>
        /// <param name="NewTopics">标题</param>
        /// <param name="Simg">小图</param>
        /// <param name="NewDetail">内容</param>
        /// <param name="NewKey">关键字</param>
        /// <param name="InDate">添加时间</param>
        /// <param name="InUser">添加人</param>
        /// <param name="NewId">新闻编号 空 为新增，其他为修改</param>
        /// <returns></returns>
        public bool News_Add(string ClsId, string ClsType, string Gid, string Pid, string NewTopics, string Simg, string NewDetail, string NewKey, string InDate, string InUser, string NewId)
        {
            SqlParameter[] pam = new SqlParameter[10];
            pam[0] = new SqlParameter("@clsid", ClsId);
            pam[1] = new SqlParameter("@clstype", ClsType);
            pam[2] = new SqlParameter("@gid", Gid);
            pam[3] = new SqlParameter("@pid", Pid);
            pam[4] = new SqlParameter("@newtopics", NewTopics);
            pam[5] = new SqlParameter("@simg", Simg);
            pam[6] = new SqlParameter("@newdetail", NewDetail);
            pam[7] = new SqlParameter("@newkey", NewKey);
            pam[8] = new SqlParameter("@indate", InDate);
            pam[9] = new SqlParameter("@inuser", InUser);

            if (NewId != "")
                SqlStr = "update hfg_news set clsid=@clsid,clstype=@clstype,gid=@gid,pid=@pid,newtopics=@newtopics,simg=@simg,newdetail=@newdetail,newkey=@newkey,indate=@indate,inuser=@inuser where newid=" + NewId + "";
            else
                SqlStr = "insert into hfg_news(clsid,clstype,gid,pid,newtopics,simg,newdetail,newkey,indate,inuser) values(@clsid,@clstype,@gid,@pid,@newtopics,@simg,@newdetail,@newkey,@indate,@inuser)";
            if (ExecSql.SqlExecNoquery(SqlStr, pam) > 0)
                return true;
            else
                return false;
        }
        #endregion
        #region 判断类别是否重复
        /// <summary>
        /// 判断类别是否重复
        /// </summary>
        public bool CanAddClass(int pid, string clsname, int clsid)
        {
            string sqlstr = "SELECT * FROM hfg_newsclass where pid=" + pid + " and clsname='" + clsname + "' and clsid<>" + clsid;
            DataSet ds = ExecSql.GetDataSet(sqlstr);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion
        #region 判断类别能否删除
        /// <summary>
        /// 判断类别能否删除
        /// </summary>
        public bool CanDelClass(int clsid)
        {
            string sqlstr = "SELECT * FROM hfg_news where clsid=" + clsid;
            DataSet ds = ExecSql.GetDataSet(sqlstr);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion
        #region 新闻列表
        public DataTable News_List()
        {
            return ExecSql.GetDataSet("select a.*,b.clsname from hfg_news a left join hfg_newsclass b on a.clsid=b.clsid").Tables[0];
        }
        #endregion
        #region 新闻删除
        public bool News_Del(string NewId)
        {
            string sql = "", msg = "";
            sql = "delete from hfg_news where newid='" + NewId + "'";
            if (ExecSql.SqlExecNoquery(SqlStr) > 0)
                return true;
            else
                return false;
        }
        #endregion
        #region 新闻类别下拉数据
        /// <summary>
        /// 获得新闻类别下拉数据
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select clsname,cast([clsid] as varchar(64))+','+cast([clstype] as varchar(64)) as clsidtype");
            strSql.Append(" FROM hfg_newsclass ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return ExecSql.GetDataSet(strSql.ToString());
        }
        #endregion
        #region 新闻类别Info数据的显示,显示一条数据
        public DataRow News_Sort_Item(string ClsId)
        {
            try
            {
                return ExecSql.GetDataSet("select * from hfg_newsclass where clsid=" + ClsId).Tables[0].Rows[0];
            }
            catch
            {
                return null;
            }
        }
        #endregion
        #region 新闻Info数据的显示,显示一条数据
        public DataRow News_Item(string NewId, int gid)
        {
            try
            {
                return ExecSql.GetDataSet("select * from hfg_news where newid=" + NewId + " and gid=" + gid.ToString()).Tables[0].Rows[0];
            }
            catch
            {
                return null;
            }
        }
        #endregion
        #region 新闻类别列表
        public DataSet News_Sort_List(string GId, string PId)
        {
            return ExecSql.GetDataSet("select * from hfg_newsclass where gid=" + GId + " and pid=" + PId + " order by clsrank");
        }
        #endregion
        #region 新闻类别新增
        public bool News_Sort_Add(int gid, int pid, string clsname, int clstype, int clsrank, string inuser)
        {
            string msg = "";
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into hfg_newsclass(");
            strSql.Append("gid,");
            strSql.Append("pid,");
            strSql.Append("clsname,");
            strSql.Append("clstype,");
            strSql.Append("clsrank,");
            strSql.Append("inuser");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(gid + ",");
            strSql.Append(pid + ",");
            strSql.Append("'" + clsname.Replace("'", "") + "',");
            strSql.Append(clstype + ",");
            strSql.Append(clsrank + ",");
            strSql.Append("'" + inuser + "'");
            strSql.Append(")");

            if (ExecSql.SqlExecNoquery(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }
        #endregion
        #region 新闻类别删除
        public bool News_Sort_Del(int clsid)
        {
            string sql = "", msg = "";
            sql = "delete from hfg_newsclass where clsid=" + clsid;
            if (ExecSql.SqlExecNoquery(sql) > 0)
                return true;
            else
                return false;
        }
        #endregion
        #region 新闻类别更新
        public bool News_Sort_Update(int clsid, int gid, int pid, string clsname, int clstype, int clsrank, string inuser)
        {
            string msg = "";
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update hfg_newsclass set ");
            strSql.Append("gid=" + gid + ",");
            strSql.Append("pid=" + pid + ",");
            strSql.Append("clsname='" + clsname + "',");
            strSql.Append("clstype=" + clstype + ",");
            strSql.Append("clsrank=" + clsrank + ",");
            strSql.Append("inuser='" + inuser + "'");
            strSql.Append(" where clsid=" + clsid + "");


            if (ExecSql.SqlExecNoquery(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }
        #endregion
    }
}
